package com.kznnc.data_structure.current;

import com.kznnc.data_structure.entity.User;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description
 * @Author houchenkai
 * @Created Date: 2021/7/1 19:55
 * @ClassName
 */
public class LockDemo {

    private static Lock lock = new ReentrantLock();
    private static volatile Integer a = 0;

    public static void main(String[] args) throws InterruptedException {
        RunnerDemo runnerDemo = new RunnerDemo();
        for (int i = 0; i <5; i++) {
            Thread thread = new Thread(runnerDemo);
            thread.start();
            thread.interrupt();
        }
        Thread.sleep(3000);
        System.out.println(a);
    }

    public static class RunnerDemo implements Runnable{
        @Override
        public void run() {
            // 在阻塞挂起期间 和获取锁以前 如果有未清空的中断标志位  记录并清空中断标志位 在获取锁后再设置中断标志位
             lock.lock();
            try {

                ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();
                for (int i = 0; i <10000 ; i++) {
/*                    if (i == 1000){
                        Thread.currentThread().interrupt();
                        System.out.println("i == 1000 设置中断");
                    }*/
                        a++;
                    if (i==1000){

                            Thread.sleep(1000);

                    }

                    }


            } catch (InterruptedException e) {
                e.printStackTrace();
                throw new RuntimeException("111");
            } finally {
                    lock.unlock();
            }
        }
    }
}
